module HwMath
import LinearAlgebra
function m_l(x, k, xo)
    n = length(xo) - 1
    tmp = 1
    for i in 1:1:(n+1)
        if (i!=k)
            tmp = tmp .* (x .- xo[i]) ./ (xo[k]-xo[i])
        end
    end
    return tmp
end

function m_int_num(f, a, b; n=500)
    h = (b-a)/n
    sum = 0
    for i in 1:1:n
        l = a + (i-1)*h
        r = l + h
        sum += h/6*(f(l)+f(r)+4*f((l+r)/2))
    end
    return sum
end

function m_f(x)
    return 1 ./ (1 .+ 25 .* x .* x)
end

function m_L(x, xo, f)
    
    n = length(xo) - 1
    sum = 0
    for i in 1:1:(n+1)
        sum = sum .+ f(xo[i]) * m_l(x, i, xo)
    end
    return sum
end

function m_L_sp(x, xo, f)
    # 不支持矩阵输入
    
    if length(x) > 1
        throw(DomainError(length(x), "length x must be 1"))
        return
    end
    n = length(xo) - 1
    sum = 0
    for i in 1:1:n
        if xo[i]<=x<=xo[i+1]
            sum = f(xo[i]) * (x-xo[i+1])/(xo[i]-xo[i+1]) + f(xo[i+1]) * (x-xo[i])/(xo[i+1]-xo[i])
            break
        end
    end
    return sum
end

end

